#ifndef __EVENT_FUNCTION_H__
#define __EVENT_FUNCTION_H__

#include "net_buffer.h"
#include "net_error.h"

namespace wel
{
	template<class CallBack>
	class CFunctionWrapper
	{
	public:
		CFunctionWrapper()
		{

		}

		CFunctionWrapper(CallBack call)
			:m_call(call)
		{

		}
	public:
		inline void operator() (CBufferWrapper buf, CNetError error);

	protected:
		CallBack m_call;
	};

	template<class CallBack>
	inline void CFunctionWrapper<CallBack>::operator() (CBufferWrapper buf, CNetError error)
	{
		m_call(buf, error);
	}

	template<class CObj, typename Functor>
	class CObjFunctor
	{
	public:
		CObjFunctor()
		{

		}
		CObjFunctor(CObj *pObj, Functor fun)
			:m_pObj(pObj)
			,m_fun(fun)
		{

		}
	public:
		inline void operator() (CBufferWrapper buf, CNetError error);
	protected:
		CObj    *m_pObj;
		Functor m_fun;
	};
	template<class CObj, typename Functor>
	inline void CObjFunctor<CObj, Functor>::operator() (CBufferWrapper buf, CNetError error)
	{
		(m_pObj->*m_fun)(buf, error);
	}

	template<typename CallBack>
	CFunctionWrapper<CallBack> bind(CallBack call)
	{
		return CFunctionWrapper<CallBack>(call);
	}


	template<class CObj, typename Functor>
	CFunctionWrapper< CObjFunctor<CObj, Functor> > bind(CObj *pObj, Functor fun)
	{
		typedef CObjFunctor<CObj, Functor> CallBack;
		return CFunctionWrapper< CallBack >( CallBack(pObj,fun) );
	}
}

#endif //__EVENT_FUNCTION_H__